﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;


/// <summary>
/// 返回結果
/// </summary>
[DataContract]
class StopRoute_Result {
    [DataMember(Order = 0)]
    public string id { get; set; }
    [DataMember(Order = 1)]
    public string name { get; set; }
    [DataMember(Order = 2)]
    public string from { get; set; }
    [DataMember(Order = 3)]
    public string to { get; set; }
    [DataMember(Order = 4)]
    public int gob { get; set; }
    [DataMember(Order =5)]
    public int estime { get; set; }
}
public partial class Service_StopRoute : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["stid"]))
        {
            using (MyDb.Db db = new MyDb.Db())
            {
                IList<StopRoute_Result> list = new List<StopRoute_Result>();
                StopRoute_Result item = null;
                string sql = "";

                if (!string.IsNullOrEmpty(Request.QueryString["gob"]))
                    sql = string.Format("select d.pathAttributeId as id,d.pathAttributeName as namezh,d.departureZh,d.destinationZh,b.goBack,e.EstimateTime as estime from stop a left join stop b on a.nameZh=b.nameZh left join PathDetail c on c.stopId=b.Id and c.city=b.city left join Route d on c.pathAttributeId=d.pathAttributeId left join EstimateTime e on c.stopId=e.StopID where a.Id='{0}' and a.city='{1}' and c.pathAttributeId is not null and d.nameZh is not null order by namezh", Request.QueryString["stid"], Request.QueryString["city"]);
                else
                    sql = string.Format("select d.pathAttributeId as id,d.pathAttributeName as namezh,d.departureZh,d.destinationZh,b.goBack,e.EstimateTime as estime from stop a left join stop b on a.nameZh=b.nameZh left join PathDetail c on c.stopId=b.Id and c.city=b.city left join Route d on c.pathAttributeId=d.pathAttributeId left join EstimateTime e on c.stopId=e.StopID where a.Id='{0}' and a.city='{1}' and c.pathAttributeId is not null and d.nameZh is not null order by namezh", Request.QueryString["stid"], Request.QueryString["city"]);
                using (DataTable dt = db.getDataTable(sql, "mess"))
                {
                    foreach (DataRow rs in dt.Rows)
                    {
                        item = new StopRoute_Result();
                        item.name = rs["nameZh"].ToString();
                        item.id = rs["ID"].ToString();
                        item.from = rs["departureZh"].ToString();
                        item.to = rs["destinationZh"].ToString();
                        item.gob = Convert.ToInt32(rs["goBack"]);
                        item.estime = string.IsNullOrEmpty(rs["estime"].ToString()) ? 0 : Convert.ToInt32(rs["estime"]); ;
                        list.Add(item);
                    }
                }
                Response.Write(string.Format("{{\"route\":{0}}}", MyDb.JSON.stringify(list)));
                list.Clear();
                list = null;
            }
        }
        Response.End();
    }
}
